/**
 * XML Parser written by Maurizio Rigamonti, DIVA Group, University of Fribourg
 * Adapted by Bruno Dumas, DIVA Group, University of Fribourg
 * 
 *
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */


package diuf.diva.hephaistk.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class XMLDocumentWriter{

  public XMLDocumentWriter(String filename, Element documentRoot, boolean addXMLHeader)
      throws Exception{
    //create file
    File file = new File(filename);
    if (file.exists()){
      if (!file.canWrite()) {
		throw new Exception("Unable to write file: " + filename);
	}
      file.delete();
    }
    //create parent directory
    /*File parent = file.getParentFile();
    if (!parent.exists()) {
		parent.mkdirs();
	}*/
    StringBuilder buffer = new StringBuilder(addXMLHeader ? "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" : "");
    writeElement(buffer, "", documentRoot);
    FileOutputStream outputStream = new FileOutputStream(file);
    OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, "UTF-8");
    String content = buffer.toString();
    outputWriter.write(buffer.toString(), 0, content.length());
    outputWriter.close();
  }

  private void writeElement(StringBuilder buffer, String offset, Element element){
    buffer.append(offset);
    buffer.append("<");
    buffer.append(element.getName());
    for (Attribute attribute: element.getAttributes()) {
		writeAttribute(buffer, attribute);
	}
    ArrayList<?> elements = element.getElements();
    if (elements.isEmpty()) {
		buffer.append("/>\r\n");
	} else{
      buffer.append(">\r\n");
      for (Object child: elements) {
		if (child instanceof Element) {
			writeElement(buffer, offset + "  ", (Element)child);
		} else{
          buffer.append(offset);
          buffer.append("  ");
          buffer.append(((CData)child).getCData());
          buffer.append("\r\n");
        }
	}
      buffer.append(offset);
      buffer.append("</");
      buffer.append(element.getName());
      buffer.append(">\r\n");
    }
  }

  private void writeAttribute(StringBuilder buffer, Attribute attribute){
    buffer.append(" ");
    buffer.append(attribute.getName());
    buffer.append("=\"");
    buffer.append(attribute.getValue());
    buffer.append("\"");
  }
}
